﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FriendsNearby.Services;
using InSTEDD.Labs.Geo;
using System.Globalization;

namespace FriendsNearby.Fbml
{
    public partial class UpdateLocation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FriendsNearbySystem system = new FriendsNearbySystem();
			IUsersService userService = system.GetService<IUsersService>();
			UserRegistry user = userService.QueryUserRegistry(x=>x.FacebookUserId == fbApplication.UserID);

            var currentLatitude = Request.QueryString["lat"];
            var currentLongitude = Request.QueryString["lon"];
            double lat = 0.0;
            double lon = 0.0; // force compile

            bool parsed = double.TryParse(currentLatitude, NumberStyles.Number, CultureInfo.InvariantCulture, out lat)
                       && double.TryParse(currentLongitude, NumberStyles.Number, CultureInfo.InvariantCulture, out lon);

            var newlocation = parsed ? new LatLong(lat, lon) : null;

            system.GetService<ILocationService>().SetCurrentLocation(user.UserID, newlocation);

            // forward request to Default.aspx page when
            // location is updated.
            Server.Transfer("Default.aspx");
        }
    }
}
